package com.wk.job.heartbeat;

public class JobStateHeartbeat {
	/* 创建心跳包，指示没有任务正在执行 */
	public JobStateHeartbeat() {
		isUnderway = false;
		isFailed = false;
	}

	/* 创建心跳包，描述正在执行的任务 */
	public JobStateHeartbeat(String jobId, double progress, boolean failed) {
		isUnderway = true;
		this.jobId = jobId;
		this.progress = progress;
		this.isFailed = failed;
	}

	@Override
	public boolean equals(Object raw) {
		if (!(raw instanceof JobStateHeartbeat)) {
			return false;
		}
		JobStateHeartbeat another = (JobStateHeartbeat) raw;

		boolean neitherUnderway = !isUnderway && !another.isUnderway;
		if (neitherUnderway) {
			return true;
		}

		boolean bothUnderway = isUnderway && another.isUnderway;
		boolean isSameJob = jobId.equals(another.jobId) && progress == another.progress && isFailed == another.isFailed;
		return bothUnderway && isSameJob;
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();

		if (isUnderway) {
			result.append(jobId);
			result.append(": ");
			result.append(progress);
			result.append(", ");
			result.append("failed?" + isFailed);
		} else {
			result.append("(none)");
		}

		return result.toString();
	}

	public boolean isUnderway;// 是否正在执行任务。如果为true，则字段jobId和progress指出正在执行的任务及其进度
	public String jobId;
	public double progress;// 进度比例（例：0.98，表示完成了98%）
	public boolean isFailed;// true：job的任一子任务失败
}
